<template>
  <div class="app-container">
    <div class="filter-container bor-container">
      <el-form size="mini" :inline="true">
        <el-form-item>
          <el-input v-model="formInline.nickName" placeholder="请输入用户昵称"></el-input>
        </el-form-item>
        <el-button size="mini" @click="query()" icon="el-icon-search">查询</el-button>
      </el-form>
    </div>
    <div class="filter-container btn-right">
      <el-button  icon="el-icon-plus" type="primary" @click="dialog.status = true" size="mini">新增用户</el-button>
    </div>
    <el-table stripe v-loading="loading" border :data="tableData">
      <el-table-column prop="id" label="用户ID" width="80px">

      </el-table-column>
      <el-table-column prop="username" label="用户名">

      </el-table-column>
      <el-table-column prop="nickName" label="用户昵称">

      </el-table-column>
      <el-table-column prop="roleName" label="角色名称">

      </el-table-column>
     <!-- <el-table-column prop="icon" label="头像" width="70px">
        <template slot-scope="scope">
          <img :src="scope.row.icon" width="50" height="50" alt="">
        </template>
      </el-table-column>-->
      <el-table-column prop="email" label="邮箱">

      </el-table-column>
      <el-table-column prop="status" label="状态">
        <template slot-scope="scope">
          <el-tag type="success" size="mini" v-if="scope.row.status === 1">启用</el-tag>
          <el-tag type="danger" size="mini" v-else>禁用</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button type="warning" size="mini" @click="openEdit(scope.row)" icon="el-icon-edit-outline">编辑</el-button>
          <el-button  size="mini" icon="el-icon-delete" type="danger" @click="onDelete(scope.row.id,scope.row.username, scope.$index)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      class="pagination-right"
      background
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="formInline.pageNum"
      :page-sizes="[10, 20]"
      :page-size="formInline.pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="formInline.total">
    </el-pagination>
    <user-edit
      v-if="dialog.status"
      :dialog_="dialog.status"
      :model="editUser"
      @close="close()"
      @success="success()"
    >
    </user-edit>
  </div>
</template>
<script>
  import { userListAPI ,deleteUserApI} from '@/api/user'
  import UserEdit from './components/UserEdit'
  import { mapGetters } from 'vuex'
  export default {
    // 系统用户管理
    name: 'SystemAdmin',
    components: {
      UserEdit
    },
    data() {
      return {
        formInline: {
          nickName: '',
          pageSize: 10,
          pageNum: 1,
          total: 0,
        },
        editUser: null, // 编辑时传入的对象
        loading: false,
        tableData: [],
        dialog: {
          status: false,
        }
      }
    },
    computed: {
      ...mapGetters([
        'buttons',
      ])
    },
    mounted() {
      this.getUserList()
    },
    methods: {
      // 编辑打开弹窗
      openEdit(obj) {
        this.editUser = obj
        this.dialog.status = true
      },
      // 点击查询事件
      query() {
        this.formInline.pageNum = 1
        this.getUserList()
      },
      // 操作成功的钩子用来更新列表
      success() {
        this.getUserList()
      },
      // 关闭弹窗返回的钩子
      close() {
        this.editUser = null
        this.dialog.status = false
      },
      // pageSize发生改变触发
      handleSizeChange(val) {
        this.pageNum = 1
        this.pageSize = val
        this.getUserList()
      },
      // pageNum 发生改变触发
      handleCurrentChange(val) {
        this.formInline.pageNum = val
        this.getUserList()
      },
      // 删除用户
      onDelete(id, usernameParam,index) {
        this.$confirm('是否删除该用户?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          deleteUserApI({id:id}).then(res => {
            if (res.code === 200) {
              this.$message.success(res.msg)
              this.getUserList()
            } else {
              this.$message.error(res.message)
            }
          })
        }).catch(() => {

        });
      },
      // 获取用户列表
      getUserList() {
        this.loading = true
        userListAPI(this.formInline).then(res => {
          debugger
          this.loading = false
          if (res.code === 200) {
            this.tableData = res.data.list
            this.formInline.total = res.data.total
          } else {
            this.$message.error(res.message)
          }
        })
      }
    }
  }
</script>

